var userCtrl  = new XFApp.client();
XFApp.client.prototype.user = {
    successCode : 10000,
    urlItems : {
        submitURL: basePath+"/userCenter/user/updatePwd",
        checkURL: basePath+"/userCenter/user/passwordCheck"
    },

    formValidate : function(){
        $("#submitForm").validate({
            errorPlacement : function(error, element) {
                if (element.is(":radio"))
                    error.appendTo(element.parent());
                else if (element.is(":checkbox"))
                    error.appendTo(element.parent().parent());
                else if(element.is(":input[name=fourcode]"))
                    error.appendTo(element.parent());
                else
                    error.insertAfter(element);
                //error.appendTo(element.parent().next());
            },
            rules: {
                oldPassword: "required",
                password:  {
                    required:true,
                    minlength:6
                },
                password2: {
                    required:true,
                    equalTo: "#password"
                }
            },
            messages: {
                oldPassword: "请输入旧密码",
                password: {
                    required:"请输入新密码",
                    minlength:$.validator.format("新密码长度必须大于{0}个字符"),
                },
                password2:{
                    required:"请再次输入密码",
                    equalTo:"两次输入的密码不一致"
                }
            }
        });
        $("#submitForm").validate();
    },

    submit: function(){
        $.ajax({
            url :_this.urlItems.checkURL,
            data : {password:$("#oldPassword").val()},
            dataType : "json",
            type : 'post',
            success : function(data) {
                if (data.code == _this.successCode) {
                    $.ajax({
                        url :_this.urlItems.submitURL,
                        data : {password:$("#password").val()},
                        dataType : "json",
                        type : 'post',
                        success : function(data) {
                            if (data.code == _this.successCode) {
                                window.webDialogUtil.alert("提示",data.msg);
                                $('#submitForm')[0].reset();
                            } else {
                                window.webDialogUtil.alert("提示",data.msg);
                            }
                        }
                    });
                } else {
                    $("#oldPassword-error").text(data.msg);
                }
            }
        });
    },



    event : function (){
        _this = this;
        $("#submitBtn").on("click",function(){
            if($("#submitForm").validate().form()){
                userCtrl.user.submit();
            }
        });
        //表单验证
        _this.formValidate();
    }
}

$(function(){
    userCtrl.user.event();
});